include("../../static/js/config.js");
include("../../static/js/common.js");

$(function () {

    var form, message = '';
    layui.use(['laydate', 'form'], function () {
        form = layui.form;
        form.verify({
            username: function (value) {
                asyncRequest("/admin/admin/checkUsername", {username:value}, function (res) {
                    if (res.code === 200){
                        if (res.data === false){
                            message = '用户名已存在，请重新输入！';
                        }
                    }
                });
                if (message !== ''){
                    return message;
                }
            }
        });

        form.on('submit(save)', function(data){
            var username = $("#username").val();
            var password = $("#password").val();

            request("/admin/admin/save", {username:username, password:password}, function (data) {
                if (data.code == 200){
                    layer.msg("操作成功", {icon: 1});
                    setTimeout(function () {
                        if (window.parent){
                            window.parent.layer.closeAll();
                            window.parent.location.reload();
                        }
                    }, 3000);
                }
            });
            return false;
        });
    })


});